from __future__ import absolute_import, unicode_literals
from django.db.models.signals import post_save, pre_delete
import pymysql

pymysql.install_as_MySQLdb()

instance_list = []


def post_save_func(sender, instance, **kwargs):
    """创建，修改"""
    if str(instance._meta.verbose_name) != '操作日志' and str(instance._meta.verbose_name) != 'django job':
        instance_data = str(instance._meta.verbose_name) + '"' + str(instance) + '"'
        if instance_list and instance:
            instance_list.clear()
        if not instance_list and instance:
            instance_list.append(instance_data)
    return instance


def delete_save_func(sender, instance, **kwargs):
    """删除"""
    instance_data = str(instance._meta.verbose_name) + '"' + str(instance) + '"'
    if instance_list:
        instance_list.clear()
    else:
        instance_list.append(instance_data)
    return instance


post_save.connect(post_save_func)  # models对象保存后触发函数
pre_delete.connect(delete_save_func)  # models对象删除后触发函数
